
/**
 * name:        个人中心-我的评价
 * author:      wuwq & lyj
 * Time:        18/01/26
 * Copyright:   chinadangmai.com
 */

// 会员基础资料
$(function(){
    $('.member_right_box').append('<div class="none" style="width:100%;height:300px;background: #fff url(../../img/base/loading.gif) center center no-repeat;"></div>')
    if($.cookie('user')!=''&&$.cookie('user')!=null){
        var token=''
        isLogin();      
        getToken();     // 获取/验证token 
        var pageSize =5;
        var url=window.location.href;
        var url_new=url.split('?')[0]
        var pageNo = url.substring(url.lastIndexOf('=')+1, url.length); 
        $.ajax({
            type:'post',
            url:new_url+'shop/member_evaluate/getEvaluationList',
            dataType:'json',
            data:{token:token,pageNo:pageNo,pageSize:pageSize},  
            success:function(e){
                if(e.pageInfo.totalPages<='1'){
                    $('.page_num li').eq(0).hide()
                    $('.page_num li').eq(1).hide()
                }
            }
        })
        // 下一页
        if(pageNo<='1'){
            $('.prev').removeClass('prev').addClass('prev1') 
        }else{
            $('.prev1').removeClass('prev1').addClass('prev')
        }
        if($.cookie('servicepageNo')==pageNo){
            $('.next').removeClass('next').addClass('next1') 
        }else{
            $('.next1').removeClass('next1').addClass('next') 
        }
        $('.page .page_num li.active a').text(pageNo);
        var page_num=parseInt(pageNo);
        $('.page .page_num li:last-child a').text(page_num+1);
        if(pageNo>=$.cookie('servicepageNo')){
            var page_num=parseInt(pageNo);
            $('.page .page_num li.active a').text(pageNo-1)  ;  
            $('.page .page_num li.active').removeClass('active') ;     
            $('.page .page_num li:last-child a').text(page_num)
            $('.page .page_num li:last-child').addClass('active')
        }
        $('.next').click(function(){
            pageNo++
            if($.cookie('servicepageNo')>=pageNo){
                $('.next').removeClass('next').addClass('next1')
                window.location.href=url_new+'?pageNo='+pageNo     
            }
        });
        $('.next1').click(function(){
            tc('已经是最后一页了')
        });
        //返回首页
        $('.prev_first').click(function(){
            window.location.href=url_new+'?pageNo='+1
        })
        // 上一页
        $('.prev').click(function(){
            pageNo--
            if(pageNo>='1'){
                window.location.href=url_new+'?pageNo='+pageNo     
            }else{
                $('.prev').removeClass('prev').addClass('prev1')
            }
        });
        $('.prev1').click(function(){
            tc('大神们这是首页不要再点了~~~')
        });
        //点击数字
        $('.page .page_num li').click(function(){
            var text=$(this).text()
            pageNo=text
            window.location.href=url_new+'?pageNo='+pageNo 
        })
        var pageSize;
        if( token!='' && token!=null){
            getEvaluationList(token,URL,pageNo,pageSize);
        }
    }else{
        window.location.href='/src/login/login.html?type=2'
    }

    // 导航栏显示已登录
    function isLogin(){
        $('.yes_or_no').remove();
        $('.mod_login').addClass('mod_login2')
        var nav=''
        nav+='<span class="hello" style="margin-left:50px;">您好，</span><span class="nike_name"></span>'
        // nav+='<span class="avater"><img src="../images/base/member_msg_pic.png" alt=""></span>'
        nav+='<i class="icon_down"></i>'
        nav+='<ul class="logined">'
        nav+='<li class="member_center"><a href="../member/member_index.html"><i></i><span>会员中心</span></a></li>'
        nav+='<li class="login_out"><a href="javascript:;"><i></i><span>退出登录</span></a></li>'
        nav+='</ul>'
        $('.mod_login').append(nav);
    }

    // 获取/验证token
    function getToken(){
        strs=$.cookie('user').split(",");
        for(var i in strs){ 
            $('.nike_name').html(strs[1]) 
            token=strs[6];
            name=strs[1];
        }
    }
    // 退出
    $('.login_out').click(function(){
        $.cookie('user', null, { path: '/' });
            $.ajax({
                type:'post',
                url:new_url+'shop/login/logOut',
                dataType:'json',
                data:'',
                success:function(data){ 
                    tc(data.message) 
                }  
            })
            window.location.reload();
    });

});

function getEvaluationList(token,URL,pageNo,pageSize){
    $.ajax({
        type:'post',
        url:new_url+'shop/member_evaluate/getEvaluationList',
        dataType:'json',
        data:{token:token,pageNo:pageNo,pageSize:pageSize},  
        success:function(e){ 
            if(e.code=='0'){
                if(e.data.length != 0 ){
                    var evaluateTpl = $("#evaluateTpl").html();
                    var pageNo= e.pageInfo.totalPages; 
                    $.cookie('servicepageNo',pageNo,{path:'/'});
                    for(var i=0; i < e.data.length; i++){
                        if(e.data[i].score == 5 || e.data[i].score == 4){
                            var score = '好评';
                        }
                        if(e.data[i].score == 1 || e.data[i].score == 2){
                            var score = '差评';
                        }
                        if(e.data[i].score == 3){
                            var score = '中评';
                        }
                        var time=e.data[i].createTime;
                        function getLocalTime(nS) {     
                            return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, " ").replace(/日/g, " ");  
                        } 
                        var evaluateListCont=template(
                            evaluateTpl, {
                                evaluateList: [{
                                    evaluationId:e.data[i].evaluationId,
                                    score:score,
                                    content:e.data[i].content,
                                    createTime:getLocalTime(time),
                                    goodsImage:e.data[i].goodsImage,
                                    goodsName:e.data[i].goodsName,
                                }]
                            }
                        );
                        $('.evaluate_list ul').append(evaluateListCont);
                        $('.page').show();
                        $('.none').hide()
                    }
                }else{
                    $('.page, .evaluate_box').remove();
                    $('.member_right_box').append('<div style="width:100%;height:200px;line-height:200px;text-align:center;color:#999;">暂无评价信息</div>');
                    $('.none').hide()
                }
            }else{
                tc(e.message);
                setTimeout(function(){
                        window.location.href='/src/login/login.html?type=2';
                },1100)
            } 
        }
    })
}